package com.blacktry.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blacktry.entity.enums.OprLogConst;
import com.blacktry.entity.model.ApiResult;
import com.blacktry.entity.model.JudgeQuestion;
import com.blacktry.entity.model.PaperManage;
import com.blacktry.service.JudgeQuestionService;
import com.blacktry.service.PaperManageService;
import com.blacktry.utils.ApiResultHandler;
import com.blacktry.utils.log.OperLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 判断题题库表 前端控制器
 * </p>
 *
 * @author blackTry
 * @since 2021-08-10
 */
@RestController
@RequestMapping("/judgeQuestion")
@Api(value = "判断题模块", tags = "判断题管理接口")
public class JudgeQuestionController {
    @Autowired
    private JudgeQuestionService judgeQuestionService;

    @Autowired
    private PaperManageService paperManageService;

    @OperLog(operModul = "判断题模块-添加",operType = OprLogConst.ADD,operDesc = "添加")
    @ApiOperation("添加")
    @PostMapping("/insert")
    public ApiResult add(@RequestBody JudgeQuestion judgeQuestion) {
        int res = judgeQuestionService.insert(judgeQuestion);
        if (res != 0) {
            return ApiResultHandler.buildApiResult(200,"添加成功",res);
        }
        return ApiResultHandler.buildApiResult(400,"添加失败",res);
    }

    @OperLog(operModul = "判断题模块-添加并保存到试卷",operType = OprLogConst.ADD,operDesc = "添加并保存到试卷")
    @ApiOperation("添加并保存到试卷")
    @PostMapping("/insertAndSavePaper/{paperId}")
    public ApiResult insertAndSavePaper(@PathVariable @DefaultValue("-1") Integer paperId,@RequestBody JudgeQuestion judgeQuestion) {
        int res = judgeQuestionService.insert(judgeQuestion);
        if (res != 0 && paperId != -1) {
            PaperManage paperManage = new PaperManage();
            paperManage.setPaperId(paperId);
            paperManage.setQuestionType(3);
            paperManage.setQuestionId(judgeQuestion.getId());
            if(paperManageService.insert(paperManage) !=0 ){
                return ApiResultHandler.buildApiResult(200,"添加成功",res);
            }else{
                return ApiResultHandler.buildApiResult(400,"添加失败",res);
            }
        }else if(paperId == -1){
            return ApiResultHandler.buildApiResult(200,"添加成功",res);
        }
        return ApiResultHandler.buildApiResult(400,"添加失败",res);
    }

    @OperLog(operModul = "判断题模块-删除指定",operType = OprLogConst.DELETE,operDesc = "删除指定")
    @ApiOperation("删除指定")
    @DeleteMapping("/delete/{id}")
    public ApiResult<Integer> delete(@PathVariable Integer id){
        return ApiResultHandler.buildApiResult(200,"成功",judgeQuestionService.delete(id));
    }

    @OperLog(operModul = "判断题模块-批量删除",operType = OprLogConst.DELETE,operDesc = "批量删除")
    @ApiOperation("批量删除")
    @DeleteMapping("/deleteBatchIds")
    public ApiResult<Integer> deleteBatchIds(String[] ids){
        return ApiResultHandler.buildApiResult(200,"成功",judgeQuestionService.deleteBatchIds(ids));
    }



    @OperLog(operModul = "判断题模块-添加/更新",operType = OprLogConst.UPDATE,operDesc = "添加/更新")
    @ApiOperation("添加/更新")
    @PostMapping("/updateOrInsert")
    public ApiResult<Integer> updateOrInsert(@RequestBody JudgeQuestion judgeQuestion){
        return ApiResultHandler.buildApiResult(200,"成功",judgeQuestionService.updateOrInsert(judgeQuestion));
    }


    @OperLog(operModul = "判断题模块-根据id查询",operType = OprLogConst.SELECT,operDesc = "根据id查询")
    @ApiOperation("根据id查询")
    @GetMapping("/select/{id}")
    public ApiResult<JudgeQuestion> select(@PathVariable Integer id){
        return ApiResultHandler.buildApiResult(200,"插入成功",judgeQuestionService.select(id));
    }


    @OperLog(operModul = "判断题模块-查看所有",operType = OprLogConst.SELECT,operDesc = "查看所有")
    @ApiOperation("查看所有")
    @GetMapping("/selectAll")
    public ApiResult<List<JudgeQuestion>> selectAll(){
        return ApiResultHandler.buildApiResult(200,"成功",judgeQuestionService.selectAll());
    }


    @OperLog(operModul = "判断题模块-分页查看所有",operType = OprLogConst.SELECT,operDesc = "分页查看所有")
    @ApiOperation("分页查看所有")
    @PostMapping("/selectAll/{page}/{size}")
    public ApiResult<Page<JudgeQuestion>> findAll(@RequestBody JudgeQuestion judgeQuestion, @DefaultValue("1") @PathVariable("page") Integer page, @DefaultValue("6") @PathVariable("size") Integer size){
        System.out.println("分页查询所有");
        Page<JudgeQuestion> judgeQuestionPage = new Page<>(page,size);
        return ApiResultHandler.buildApiResult(200, "请求成功！", judgeQuestionService.selectPage(judgeQuestion,judgeQuestionPage));
    }
}

